var NetSocket = require("NetSocket");
var Observer = require('Observer');
var PrefabLoader = require('PrefabLoader');
var GameData = require('GameData');

cc.Class({
    extends: Observer,

    properties: {
        numLabelArr: {default: [], displayName: "房间号", type: cc.Label},
        _numArr: [],
    },

    // use this for initialization
    onLoad: function () {
        this._initMsg();
        this._numArr = [];
        this._updateNum();
    },
    _onMsg(msg, data){
        if (msg == MJNetMsg.recv.JoinRoom.msg) {
            if (data.result) {
                var roomID = this._getInputRoomID();
                GameData.setRoomID(roomID);
                console.log("加入房间成功:" + roomID);
            } else {
                this.onClickClean();
            }
        }
    },
    _getMsgList(){
        return [
            MJNetMsg.recv.JoinRoom.msg,
        ];
    },
    onClickNum(node, data){
        if (data != "" && data.length > 0 && this._numArr.length < 6) {
            this._numArr.push(data);
            this._updateNum();

            if (this._numArr.length == 6) {
                // 可以进入房间
                var roomID = this._getInputRoomID();
                NetSocket.send(MJNetMsg.send.JoinRoom, {roomId: roomID,});
            }
        }
    },
    _getInputRoomID(){
        var roomID = "";
        for (var i = 0; i < this._numArr.length; i++) {
            roomID += this._numArr[i];
        }
        return roomID;
    },
    _updateNum(){
        var len = this._numArr.length;
        for (var i = 0; i < this.numLabelArr.length; i++) {

            var item = this.numLabelArr[i];
            if (i < len) {
                item.string = this._numArr[i];
            } else {
                item.string = "";
            }
        }
    },
    onClickClose(){
        this.node.destroy();
    },
    // 回删
    onClickBackSpace(){
        var len = this._numArr.length;
        if (len > 0) {
            this._numArr.splice(len - 1, 1);
            this._updateNum();
        }
    },
    // 清空
    onClickClean(){
        this._numArr = [];
        this._updateNum();
    },

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
